#include "qt_utils.h"


QIcon newIcon(const QString& iconName)
{
	QString iconsDir = QDir::cleanPath(QDir::currentPath() + "/../icons");
	QString iconPath = QString(":/%1/%2.png").arg(iconsDir, iconName);
	return QIcon(iconPath);
}

QAction* newAction(QObject* parent, const QString& text, const std::function<void()>& slot, const QVariant& shortcut,
                   const QString& icon, const QString& tip, const bool checkable, const bool enabled, const bool checked)
{

	QAction* action = new QAction(text, parent);


	if (!icon.isNull()) {
		//action->setIcon(icon);
		//action->setIconText(text.replace(" ", "\n"));
		action->setIcon(newIcon(icon));
		action->setIconText(text);
	}


	if (shortcut.isValid()) {
		if (shortcut.canConvert<QStringList>()) {

			QStringList shortcutList = shortcut.toStringList();
			QList<QKeySequence> keySequences;
			for (const QString& sc : shortcutList) {
				keySequences.append(QKeySequence(sc));
			}
			action->setShortcuts(keySequences);
		} else if (shortcut.canConvert<QString>()) {

			action->setShortcut(QKeySequence(shortcut.toString()));
		}
	}

	if (!tip.isEmpty()) {
		action->setToolTip(tip);
		action->setStatusTip(tip);
	}


	if (slot) {
		QObject::connect(action, &QAction::triggered, slot);
	}


	action->setCheckable(checkable);
	action->setEnabled(enabled);
	action->setChecked(checked);

	return action;
}
